<template>
  <div>
    <el-row>
      <el-col :span="2"
        ><div class="base-line text-center"><i class="el-icon-key"></i></div
      ></el-col>
      <el-col :span="17"><div class="base-line">修改当前用户密码</div></el-col>
      <el-col :span="4">
        <el-button type="primary" @click="dialogVisible = true"
          >立即修改</el-button
        >
      </el-col>
    </el-row>
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="30%"
      :append-to-body="true"
    >
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
      >
        <el-form-item label="原密码" prop="lastPass">
          <el-input
            type="password"
            v-model="ruleForm.lastPass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="pass">
          <el-input
            type="password"
            v-model="ruleForm.pass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input
            type="password"
            v-model="ruleForm.checkPass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleChangePass">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { changePass } from "@/api/user";

export default {
  computed: {
    ...mapGetters(["account"]),
  },
  data() {
    var validatePass = (rule, value, callback) => {
      if (value.length < 5) {
        callback(new Error("密码不能小于5位"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        pass: "",
        checkPass: "",
        lastPass: "",
      },
      rules: {
        lastPass: [{ validator: validatePass, trigger: "blur" }],
        pass: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
      },
      dialogVisible: false,
    };
  },
  methods: {
    handleChangePass() {
      this.$refs.ruleForm.validate((valid) => {
        if (!valid) {
          this.$message.warning("error");
          return false;
        }
        changePass({
          account: this.account,
          ...this.ruleForm,
        }).then((res) => {
          this.dialogVisible = false;
        });
      });
    },
    resetForm() {
      this.$refs.ruleForm.resetFields();
    },
  },
};
</script>

<style lang="scss" scoped>
.base-line {
  line-height: 36px;
}
</style>